<template>
  <div>
      <Button v-hasC="namebutshow.but" type="primary" class="ivu-mb-8" @click="handleupdate(true)">添加配置</Button>
      <JG-table
          :data="namedata"
          :total="total"
          :butshow="namebutshow"
          :loading="nameloading"
          :columns="namecolumns"
          :disabledhover="true"
            :maxHeight="600"
          @handleDelete="handleDelete1"
          @handleConfig="classhandleEdit"
          @handleEdit="handleEdit">
      </JG-table>
       <Modal
            v-model="update"
            :title="edit?'添加配置项':'编辑配置项'"
            @on-cancel="canceledit"
            >
            <Form ref="modalname" :model="params" :rules="ruleInline" :label-width="130">
                <FormItem label="配置选项名称：" prop="name" >
                    <Input v-model="params.name" :maxlength="30" placeholder="请填写配置选项名称"  style="width:280px"/>
                </FormItem>
                 <FormItem label="配置选项标识：" prop="id_value" >
                    <Input v-model="params.id_value" placeholder="请填写配置选项标识"  style="width:280px"/>
                </FormItem>
                <FormItem label="是否可配置：">
                    <RadioGroup v-model="params.is_config" style="margin-left:10px">
                        <Radio :label="1">是</Radio>
                        <Radio :label="0">否</Radio>
                    </RadioGroup>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button @click="canceledit">取消</Button>
                <Button  type="primary" :loading="modal_loading"  @click="submitedit">保存</Button>
            </div>
        </Modal>
        <Modal
            v-model="modaldel"
            title="删除提醒"
            @on-cancel="cancel" width="420">
            <p>确定要删除当前配置？删除后数据将无法恢复</p>
            <div slot="footer">
                <Button type="error" :loading="modal_loading" @click="del">确定删除</Button>
                <Button @click="cancel">取消</Button>
            </div>
        </Modal>
        <classmodal ref="classmodal" @askData="askData" :butshow="classbutshow" :moduledata="moduledata"/>
  </div>
</template>
<script>
    import { swtswtgetswt, swtswtupdateswt, swtswtdeleteswt, swtswtdeleteswtvalue } from '@api/business'
    import classmodal from './classmodal';
    export default {
        name: 'tablename3',
        components: { classmodal },
        data () {
            return {
                modalname: 'modalname',
                total: -1,
                nameloading: false,
                namecolumns: [
                    { title: '配置名称', key: 'name', minWidth: 100 },
                    { title: '配置标识', key: 'id_value', minWidth: 100 },
                    { title: '操作', slot: 'businessAdaction3', fixed: 'right', width: 180 }
                ],
                // namedata: [],
                namedata: [],
                namebutshow: {
                    config: {
                        name: '-v1-swt-swt-detail-get-swt-detail',
                        flag: '/v1/swt/swt-detail/get-swt-detail'
                    },
                    but: {
                        name: '-v1-swt-swt-update-swt',
                        flag: '/v1/swt/swt/update-swt'
                    },
                    Delete: {
                        name: '-v1-swt-swt-delete-swt2',
                        flag: '/v1/swt/swt/delete-swt'
                    }
                },
                params: { id: 0, name: '', id_value: '', is_default: 0, is_config: 1 },
                ruleInline: {
                    id_value: [{ required: true, message: '请填写配置标识', trigger: 'blur' }],
                    name: [{ required: true, message: '请填写配置选项名称', trigger: 'blur' }]
                },
                update: false,
                modaldel: false,
                modal_loading: false,
                edit: true,
                classbutshow: {
                    add: {
                        name: '-v1-swt-module-update-module-detail',
                        flag: '/v1/swt/module/update-module-detail'
                    },
                    Edit: {
                        name: '-v1-swt-module-update-module-detail2',
                        flag: '/v1/swt/module/update-module-detail'
                    },
                    Delete: {
                        name: '-v1-swt-module-delete-module-detail',
                        flag: '/v1/swt/module/delete-module-detail'
                    }
                },
                moduledata: []
            };
        },

        mounted () {
            this.initial()
        },

        methods: {
            initial () {
                this.askData()
            },
            askData () {
                this.nameloading = true
                swtswtgetswt().then(res => {
                    this.namedata = res
                    // this.listdata = this.namedata.filter(ee => {
                    //     return +ee.is_default !== 0
                    // });
                    this.nameloading = false
                })
            },
            handleDelete1 (row) {
                this.params.id = row.id
                this.edit = true
                this.modaldel = true
            },
            handleEdit (row) {
                this.params.id = row.id
                this.params.name = row.name
                this.params.is_config = row.is_config * 1
                this.params.id_value = row.id_value
                // this.params.is_default=row.is_default
                // this.edit = true
                this.update = true
            },
            classhandleEdit (bool) {
                this.$refs.classmodal.initial(bool)
            },
            cancel () {
                this.params = { id: 0, name: '', value: '', id_value: '', is_config: 1 }
                this.modaldel = false
            },
            del () {
                if (this.edit) {
                    swtswtdeleteswt({ id: this.params.id }).then(res => {
                        this.$Message.success('删除配置成功');
                        this.cancel()
                        this.askData()
                    })
                } else {
                    swtswtdeleteswtvalue({ id: this.params.id }).then(res => {
                        this.$Message.success('删除默认成功');
                        this.cancel()
                        this.askData()
                    })
                }
            },
            handleupdate (bool) {
                this.edit = bool
                // if (bool) this.params.is_default = 0
                // else this.params.is_default = 1
                this.update = true
            },
            canceledit () {
                this.$refs.modalname.resetFields();
                this.params = { id: 0, name: '', value: '', id_value: '', is_config: 1 }
                this.update = false
            },
            submitedit () {
                this.$refs.modalname.validate((valid) => {
                    if (valid) {
                        this.modal_loading = true
                        if (this.edit) {
                            swtswtupdateswt(this.params).then(res => {
                                this.$Message.success('编辑成功');
                                this.canceledit()
                                this.askData()
                                this.modal_loading = false
                            })
                        } else {
                            swtswtupdateswt(this.params).then(res => {
                                this.$Message.success('添加成功');
                                this.canceledit()
                                this.askData()
                                this.modal_loading = false
                            })
                        }
                    }
                })
            }
        }
    };
</script>
